package com.hnkywl.sts.service.base;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.base.BmDao;
import com.hnkywl.sts.dao.base.JkpzDao;
import com.hnkywl.sts.entity.base.Dxpz;
import com.hnkywl.sts.entity.base.Jkpz;
import com.hnkywl.sts.entity.sys.Bm;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-17
 * Time: 下午2:54
 * To change this template use File | Settings | File Templates.
 */
@Service
public class JkpzService {
    private JkpzDao jkpzDao;

    public String findJkpzList(String bm, int pageNo, int pageSize) {
        Page<Jkpz> page = this.jkpzDao.findJkpzList(bm, pageNo, pageSize);
        List<Jkpz> list = page.getResult() == null ? new ArrayList<Jkpz>() : page.getResult();
        String json = BaseUtils.fetchCollectionToJson(list, "id", "bm", "flag");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "]}";
    }

    @Transactional
    public void addJkpz(Jkpz jkpz) throws RuntimeException {
        jkpz.setLastUpdated(new Date());
        jkpz.setCreateUser(YgService.getCurrentYg());
        jkpz.setCreateOrgan(YgService.getCurrentYg().getZggs());
        jkpz.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.jkpzDao.save(jkpz);
    }

    @Transactional
    public void changeJkpz(Long id) throws RuntimeException {
        Jkpz j = this.jkpzDao.get(id);
        if (j.getFlag()) {
            j.setFlag(false);
        } else {
            j.setFlag(true);
        }
        j.setLastUpdated(new Date());
        this.jkpzDao.save(j);
    }


    @Autowired
    public void setJkpzDao(JkpzDao jkpzDao) {
        this.jkpzDao = jkpzDao;
    }
}
